package com.cv4j.app.fragment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.filters.CommonFilter;
import com.cv4j.image.util.Preconditions;
import com.cv4j.rxcv4j.RxImageData;

/**
 * 空间卷积
 *
 * @since 2021-05-14
 */
public class SpitalConvFragment extends BaseFragment {
    private Image image1;
    private Image image2;
    private Image image3;
    private Image image4;
    private Image image5;

    private Image image6;
    private Image image7;
    private Image image8;
    private Image image9;
    private Image image10;

    private Image image11;
    private Image image12;
    private Context mContext;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_spital_conv, container, false);
        mContext = root.getContext();
        initView(root);
        return root;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    private void initView(Component root) {
        image1 = (Image) root.findComponentById(ResourceTable.Id_image1);
        image2 = (Image) root.findComponentById(ResourceTable.Id_image2);
        image3 = (Image) root.findComponentById(ResourceTable.Id_image3);
        image4 = (Image) root.findComponentById(ResourceTable.Id_image4);
        image5 = (Image) root.findComponentById(ResourceTable.Id_image5);

        image6 = (Image) root.findComponentById(ResourceTable.Id_image6);
        image7 = (Image) root.findComponentById(ResourceTable.Id_image7);
        image8 = (Image) root.findComponentById(ResourceTable.Id_image8);
        image9 = (Image) root.findComponentById(ResourceTable.Id_image9);
        image10 = (Image) root.findComponentById(ResourceTable.Id_image10);

        image11 = (Image) root.findComponentById(ResourceTable.Id_image11);
        image12 = (Image) root.findComponentById(ResourceTable.Id_image12);
        setPixMapImage(image1, "原图");
        setPixMapImage(image2, "ConvolutionHV");
        setPixMapImage(image3, "MinMax");
        setPixMapImage(image4, "SAPNoise");
        setPixMapImage(image5, "Sharp");

        setPixMapImage(image6, "Medima");
        setPixMapImage(image7, "Laplas");
        setPixMapImage(image8, "FindEdge");
        setPixMapImage(image9, "Sobel");
        setPixMapImage(image10, "Variance");

        setPixMapImage(image11, "MaerOperator");
        setPixMapImage(image12, "USM");
    }

    private void setPixMapImage(Image image1, String filterName) {
        if ("原图".equals(filterName)) {
            image1.setPixelMap(ResourceTable.Media_test_spital_conv);
            return;
        }
        if (Preconditions.isNotBlank(filterName)) {
            CommonFilter filter = getFilter(filterName);
            RxImageData.bitmap(PixelMapUtil.getPixelMapFromResource(
                mContext, ResourceTable.Media_test_spital_conv).get())
                .addFilter(filter)
                .into(image1);
        }
    }

    private CommonFilter getFilter(String filterName) {
        CommonFilter object = null;
        try {
            object = (CommonFilter) Class.forName("com.cv4j.core.spatial.conv." + filterName + "Filter").newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        return object;
    }
}
